var restify=require('restify'),config=require('./config'),stc=require('./controller/static'),fs = require('fs'),
	log = require('bunyan').createLogger({
		name:'server',level:'info',serializers: restify.bunyan.serializers,
		streams: [
			{
				level: 'info',
				stream: process.stdout
				//,path: '/inclog/info.log'
			},{
				level: 'error',
				//stream: process.stdout,
				path: '/inclog/error.log'
			}
			]
	}),server=restify.createServer({name:'incloud',log:log})
	//,server=restify.createServer({name:'incloud',log:log,certificate:fs.readFileSync(config.ssl.certificate,'ascii'),key:fs.readFileSync(config.ssl.key,'ascii')})
/*	
http.get(new RegExp('.*'),function (req, res, next){
	if (!req.headers || !req.headers['host'])res.send(400)
	res.header('Location','https://'+req.headers.host.split(':')[0]+':'+config.sslport+req.url)
	res.send(302)
});http.listen(config.port,config.host,function(){log.info('HTTP Server started on %s:%s',config.host,config.port)})
*/
server.use(restify.queryParser())
server.use(restify.acceptParser(server.acceptable))
server.get(/^.*\.(html|png|svg|css|js|ico)$/,stc.file)
server.get('/',stc.file)// /^(?!(\/api\/)).*$/
require('./controller')(server)
server.on('after', function (req, res, route, error){req.log.info({err: error,url: req.url,route: route,params: req.params})})
server.on('uncaughtException', function (req, res, route, error) {req.log.fatal({err: error,url: req.url,route: route,params: req.params});res.send(error)})
server.listen(config.sslport,config.host,function(){log.info('HTTPS Server started on %s:%s',config.host,config.sslport)})
process.on('uncaughtException', function(e){log.fatal(e, 'Uncaught Exception')})